package com.yuan.zs.ui.account;

import android.text.TextUtils;
import android.widget.EditText;

import com.yuan.base.framework.BaseFrameworkActivity;
import com.yuan.base.framework.bean.TipResult;
import com.yuan.base.framework.http.BaseHttpCallBack;
import com.yuan.base.framework.http.BaseHttpRequest;
import com.yuan.base.utils.PreferencesUtils;
import com.yuan.zs.Constant;
import com.yuan.zs.R;
import com.yuan.zs.Urls;

import java.util.HashMap;

/**
 * 修改密码
 */
public class ModifyPwdActivity extends BaseFrameworkActivity {
	// --------------------------控件--------------------------
	private EditText et_old_pwd;
	private EditText et_new_pwd;
	private EditText et_new_pwd2;

	@Override
	protected int getLayoutId() {
		return R.layout.activity_modify_pwd;
	}

	@Override
	protected void bindViews() {
		et_old_pwd = findViewById(R.id.et_old_pwd);
		et_new_pwd = findViewById(R.id.et_new_pwd);
		et_new_pwd2 = findViewById(R.id.et_new_pwd2);
		setOnClickListener(R.id.btn_submit, v -> submit());
	}

	private void submit() {

		String oldPwd = PreferencesUtils.getString(this, Constant.Preferences.USER_PWD);
		if (!getStringByUI(et_old_pwd).equals(oldPwd)) {
			showToast("旧密码输入错误,请重新输入!");
			return;
		}
		if (TextUtils.isEmpty(getStringByUI(et_new_pwd)) || TextUtils.isEmpty(getStringByUI(et_new_pwd2))) {
			showToast("新密码不能为空!");
			return;
		}
		if (!getStringByUI(et_new_pwd).equals(getStringByUI(et_new_pwd2))) {
			showToast("两次输入的新密码不一致,请重新输入!");
			return;
		}

		HashMap<String, Object> params = new HashMap<>();
		params.put("password", getStringByUI(et_new_pwd));
		BaseHttpRequest.post(Urls.getUrl(Urls.modifyPwd), params, new BaseHttpCallBack<TipResult>() {
			@Override
			public void onPreStart() {
				showProgressBar("正在提交,请稍后...");
			}

			@Override
			public void onPost() {
				hideProgressBar();
			}

			@Override
			public void onSuccess(TipResult result) {
				if (result.isSuccess()) {
					PreferencesUtils.putString(ModifyPwdActivity.this, Constant.Preferences.USER_PWD, getStringByUI(et_new_pwd));
					showToast("密码修改成功!");
					closeSelf();
				} else {
					showToast(result.getMessage());
				}
			}

		});
	}

}
